// pages/getDizhi/getDizhi.js
import api from '../../utils/api.js'
var app = getApp();
var QQMapWX = require("../../utils/qqmap-wx-jssdk.js");
var qqmapsdk;
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'

let http = new HTTP()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        allDizhi: [],
        current: "",
        status: true,
        imgUrl: config.imgUrl,
        address: ""

    },
    //获取所有地址
    getAll_address(obj) {
        http.request({
            url: api.allAddress,
            data: {
                user_id: wx.getStorageSync("user_id")
            },
            success: (res) => {
                console.log(res)
                if (res.code == 200) {

                    this.setData({
                        allDizhi: res.data,
                    })
                    let integralAddress = wx.getStorageSync('integralAddress')
                    if (!integralAddress) {
                        let address = res.data.reverse();
                        wx.setStorageSync('integralAddress', address[0]);
                    }


                } else {
                    this.setData({
                        allDizhi: [],
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var order = options.order
        var lat = options.lat;
        var lon = options.lon;
        if (lat && lon) {
            this.setData({
                fromAddress: {
                    lat: lat,
                    lon: lon
                }
            })
        }
        this.setData({
            order: order,
        })
    },
    addDizhi() {
        wx.navigateTo({
            url: "/pages/addDizhi/addDizhi",
        })
    },
    binaji(e) {
        var dizhi = this.data.allDizhi.filter((item) => {
            return item.id == e.target.dataset.id
        })[0]
        dizhi = JSON.stringify(dizhi)
        if (this.data.allDizhi.length == 1) {
            wx.navigateTo({
                url: "/pages/changeDizhi/changeDizhi?dizhi=" + dizhi + "&status=1",
            })
        } else {
            wx.navigateTo({
                url: "/pages/changeDizhi/changeDizhi?dizhi=" + dizhi + "&status=0",
            })
        }
    },
    change(e) {
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2].route;
        console.log('prevPage', prevPage);
        if (prevPage.indexOf('/my/') > 0) {
            return;
        }

        var dizhi = this.data.allDizhi.filter((item) => {
            return item.id == e.target.dataset.id
        })[0]
        console.log(e)
        console.log(this.data.allDizhi)
        var address = {
            city_id: dizhi.city_id,
            id: dizhi.id,
            address: dizhi.address,
            detail: dizhi.detail,
            province: dizhi.province,
            city: dizhi.city,
            district: dizhi.district,
            name: dizhi.name,
            phone: dizhi.mobile,
        }
        wx.setStorageSync("integralAddress", address)

        app.globalData.pass = true
        setTimeout(() => {
            wx.navigateBack({
                delta: 1
            })
        }, 200)
        return;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getAll_address()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})